/** \brief File implementing a simple grid made of hexagons for the game of
 * life.
 *
 * This file contains the implementation of a class implementing a simple grid
 * for the game of life, where the cells are hexagons.
 *
 * @file HexagonGrid.cpp
 * @author Johan "Solo" Luisier
 * @date 2013/08/24
 */
#include "HexagonGrid.hpp"

#include "Cell.hpp"

#include <QtCore/QDebug>

const quint32 HexagonGrid::MaxNeighbourNumber( 6u );

HexagonGrid::HexagonGrid( const quint32 &w, const quint32 &h, QObject *parent )
    : GridBase( w, h, parent )
{}

HexagonGrid::~HexagonGrid()
{}

bool HexagonGrid::initialiseNeighbours()
{
    quint32 i, j;

    /**
     * The complicated pattern for border Cell instances is somehow delegated
     * to the GridBase::operator(), which returns zero for unvalid indices.
     */
    for ( i = 0u; i < height(); i++ )
    {
        for ( j = 0u; j < width(); j++ )
        {
            operator()( i, j ) -> addNeighbour( operator()( i, j - 1u ) );
            operator()( i, j ) -> addNeighbour( operator()( i, j + 1u ) );
            if ( i % 2u == 1u )
            {
                operator()( i, j ) -> addNeighbour( operator()( i - 1u, j - 1u ) );
                operator()( i, j ) -> addNeighbour( operator()( i - 1u, j ) );
                operator()( i, j ) -> addNeighbour( operator()( i + 1u, j - 1u ) );
                operator()( i, j ) -> addNeighbour( operator()( i + 1u, j ) );
            }
            else
            {
                operator()( i, j ) -> addNeighbour( operator()( i - 1u, j ) );
                operator()( i, j ) -> addNeighbour( operator()( i - 1u, j + 1u ) );
                operator()( i, j ) -> addNeighbour( operator()( i + 1u, j ) );
                operator()( i, j ) -> addNeighbour( operator()( i + 1u, j + 1u ) );
            }
        }
    }

    for ( i = 0u; i < height(); i++ )
    {
        for ( j = 0u; j < width(); j++ )
        {
            operator()( i, j ) -> setLifeRules( GameRules );
        }
    }

    qDebug() << tr( "HexagonGrid::initialiseNeighbours specific initialisation done" );

    CellNumber = width() * height();

    return true;
}
